package com.ruoyi.system.youshuda.config;

import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
import com.ruoyi.common.utils.LongJsonSerializer;
import org.springframework.boot.autoconfigure.jackson.Jackson2ObjectMapperBuilderCustomizer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder;

@Configuration
public class JacksonConfig {

    @Bean("jackson2ObjectMapperBuilderCustomizer")
    public Jackson2ObjectMapperBuilderCustomizer jackson2ObjectMapperBuilderCustomizer() {
        Jackson2ObjectMapperBuilderCustomizer customizer = new Jackson2ObjectMapperBuilderCustomizer() {
            @Override
            public void customize(Jackson2ObjectMapperBuilder jacksonObjectMapperBuilder) {
                /**
                 * 当json转换时，遇到Long时转换成字符串，防止前端拿到数据精度丢失
                 */
                jacksonObjectMapperBuilder.serializerByType(Long.class, new LongJsonSerializer())
                        .serializerByType(Long.TYPE, new LongJsonSerializer());
            }
        };
        return customizer;
    }
}
